package com.orangelife.mobile.surrounding.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.curry.android.net.NetworkUtil;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.ImageDownloader.ImageSize;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.surrounding.activity.HotGoodsDetailActivity;
import com.curry.android.adapter.FatherAdapter;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 商品分类的适配器
 * @author yuyuxia
 *
 */
public class GoodsClassifyListViewAdapter<T> extends FatherAdapter<T> {

	private Context context;
	private List<Map<String,Object>> list;
	private ImageDownloader image= ImageDownloader.getImageDownloader();
	private boolean flag;

	public GoodsClassifyListViewAdapter(Context context, List<Map<String, Object>> list,boolean flag){
		
		this.context = context;
		this.list = list;
		this.flag = flag;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_lv_circumtesco_hotgoods, null);
			holder.ivPhoto = (ImageView) convertView.findViewById(R.id.ivPhoto);
			holder.tvSalesVolume = (TextView) convertView.findViewById(R.id.tvSalesVolume);
			holder.tvGoodsName = (TextView) convertView.findViewById(R.id.tvGoodsName);
			holder.tvStandard = (TextView) convertView.findViewById(R.id.tvStandard);
			holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
			holder.tvPrice2 = (TextView) convertView.findViewById(R.id.tvPrice2);
			holder.llClick = (LinearLayout) convertView.findViewById(R.id.llHotGoods);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		/*listview填数据*/
		Map<String,Object> map=list.get(position);

		if(flag){
			holder.tvSalesVolume.setVisibility(View.VISIBLE);
			holder.tvSalesVolume.setText("月售"+map.get("sales").toString());
		}else{
			holder.tvSalesVolume.setVisibility(View.GONE);
		}

		//现价的中划线
		holder.tvGoodsName.setText(map.get("goodsName").toString());
		holder.tvStandard.setText(map.get("pdtDesc").toString());
		holder.tvPrice.setText(Constant.RMB_FLAG+map.get("price").toString());
		holder.tvPrice2.setText(Constant.RMB_FLAG+map.get("mkPrice").toString());
		holder.tvPrice2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		String url=map.get("goodsLogo").toString();
		image.download(url, holder.ivPhoto,ImageSize.IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG);
		holder.llClick.setOnClickListener(new listener(map));
		return convertView;
		
	}
	class listener implements View.OnClickListener{

		private  Map<String,Object> map=null;
		public listener(Map<String, Object> map){
			this.map = map;
		}
		@Override
		public void onClick(View view) {
			if (!NetworkUtil.isConnectedNet(context)) {
				Toast.makeText(context,
						context.getResources().getString(R.string.net_disconnected),
						Toast.LENGTH_SHORT).show();
				return;
			}
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putSerializable("detail", (Serializable) map);
			intent.setClass(context, HotGoodsDetailActivity.class);
			intent.putExtras(bundle);
			context.startActivity(intent);
		}
	}
	class ViewHolder{
		/**商品名称*/
		private TextView tvGoodsName;
		/**规格*/
		private TextView tvStandard;
		/**现价*/
		private TextView tvPrice;
		/**原价*/
		private TextView tvPrice2;
		/**月销量*/
		private TextView tvSalesVolume;
		/**点击事件*/
		private LinearLayout llClick;
		/**商品图片*/
		private ImageView ivPhoto;
	}
	@Override
	public BaseAdapter setList(List<T> list) {
		this.list = (List<Map<String,Object>>) list;
		return GoodsClassifyListViewAdapter.this;
	}

}
